package com.glamour.hlcity.common.util.queue;

import java.util.List;

import com.glamour.hlcity.common.model.Node;

/**
 * 循环队列
 * @author zhaoxing.liuzx
 *
 */
public class CircleQueue {
	private int size;
	private Node[] nodes;
	private int front;
	private int tail;
	public CircleQueue(int size)
	{
		this.size=size;
		nodes=new Node[size];
		front=tail=0;
		
	}
    public void addNode(Node node)
    {
    	if(!isFull())
    	
    	{
    		 tail = (tail+1) % size;			
    	     nodes[tail] =node;	

    	}
    	
    }
    public Node topNode()
    {
    	if(!isEmpty())
    	{
    		front = ( front+1) % size;			
    	     return nodes[front];

    	}
    	return null;
    }
    public boolean isFull()
    {
    	return (tail+1) % size == front;
    }
    public boolean isEmpty()
    {
    	return front==tail;
    }
    public int size()
    {
    	return (tail-front+size)%size;
    }

    /**
     * 批量新增节点
     * @param nodeList
     */
    public void addNodes(List<Node> nodeList){
    	for(Node node:nodeList){
    		addNode(node);
    	}
    }
}
